import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:robotcontrol/entity/category_model.dart';
import 'package:robotcontrol/entity/common_item.dart';
import 'package:robotcontrol/http/http_manager.dart';
import 'package:robotcontrol/http/url.dart';
import 'package:robotcontrol/utils/navigator_util.dart';

class CategoryDetailPage extends StatefulWidget{
  final CategoryModel categoryModel;
  const CategoryDetailPage({Key key,this.categoryModel}):super(key: key);
  _CategoryDetailPageState createState()=> _CategoryDetailPageState();
}
class _CategoryDetailPageState extends State<CategoryDetailPage> with AutomaticKeepAliveClientMixin{
  Issue issue;
  List<Item> list;
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    pullPage();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return CustomScrollView(
      slivers: [
        _sliverAppBar(),
        _sliverList(widget.categoryModel)
      ],
    );
  }
  Widget _sliverAppBar(){
    return SliverAppBar(
      // 左侧控件，通常情况下为"返回"图标
      leading: GestureDetector(
        onTap: ()=>back(),
        child: Icon(Icons.arrow_back,color: Colors.black,),
      ),
      // 阴影
      elevation: 2,
      //亮度
      brightness: Brightness.light,
      //背景色
      backgroundColor: Colors.white,
      //展开区域的高度
      expandedHeight: 200,
      // 设置为true时，当SliverAppBar内容滑出屏幕时，将始终渲染一个固定在顶部的收起状态
      pinned: true,
      //展开和折叠区域
      flexibleSpace: _flexibleSpace(),
    );
  }
  ////展开和折叠区域  弹性空间
 Widget _flexibleSpace(){
    return LayoutBuilder(builder: (context,constraints){
      changeExpendStatus(MediaQuery.of(context).padding.top.toInt()+56,
      constraints.biggest.height.toInt());
    });
  }
  // 判断是否展开，从而改变字体颜色
  bool isExpend = true;
  void changeExpendStatus(int statusBarHeight,int offset){
    if(offset >statusBarHeight && offset <250){
      if(!isExpend){
        isExpend = true;
      }
    }else{
      if(isExpend){
        isExpend = false;
      }
    }
  }
  //列表
  Widget _sliverList(CategoryModel categoryModel){
    return SliverList(
        delegate: SliverChildBuilderDelegate(
            (context,index){
      return ListItemWidget();
    }));
  }
  void pullPage() async{
    HttpManager.getData(Url.categoryVideoUrl, success: (json) {
      issue = Issue.fromJson(json);
      loadMorePage();
      setState(() {
      });
    });
  }
  void loadMorePage() async{
    HttpManager.getData(issue.nextPageUrl +
        "&udid=d2807c895f0348a180148c9dfa6f2feeac0781b5&deviceModel=Android", success: (json) {
      issue = Issue.fromJson(json);
      list.addAll(issue.itemList);
      setState(() {
      });
    });
  }
}